package com.itcat.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.OmsCompanyAddressApi;
import com.itcat.api.mysql.OmsOrderReturnApplyApi;
import com.itcat.common.CommonPage;
import com.itcat.mysql.param.OmsOrderReturnApplyParam;
import com.itcat.mysql.pojo.OmsCompanyAddress;
import com.itcat.mysql.pojo.OmsOrderReturnApply;
import com.itcat.mysql.vo.OmsOrderReturnApplyVo;
import com.itcat.service.OmsOrderReturnApplyService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Component
@Service
public class OmsOrderReturnApplyServiceImpl implements OmsOrderReturnApplyService {

    @DubboReference
    private OmsOrderReturnApplyApi omsOrderReturnApplyApi;

    @DubboReference
    private OmsCompanyAddressApi omsCompanyAddressApi;

    /**
     * 退货申请分页查询
     *
     * @param omsOrderReturnApplyParam
     * @return
     */
    public CommonPage<OmsOrderReturnApply> OrderReturnApply(OmsOrderReturnApplyParam omsOrderReturnApplyParam) {
        //调用api查询分页
        Page<OmsOrderReturnApply> pageResult = omsOrderReturnApplyApi.getPage(omsOrderReturnApplyParam);
        return CommonPage.restPage(pageResult);

    }

    /**
     * 批量删除退货申请
     *
     * @param ids
     */
    public void delete(Integer[] ids) {

        omsOrderReturnApplyApi.delete(ids);

    }

    /**
     * 查看退货申请详情
     *
     * @param id
     * @return
     */
    public OmsOrderReturnApplyVo returnApplyDetail(Integer id) {
        //查询退货申请详情
        OmsOrderReturnApply omsOrderReturnApply = omsOrderReturnApplyApi.findReturnApply(id);
        //获取地址id查询地址详情
        OmsCompanyAddress address = omsCompanyAddressApi.findOmsCompanyAddress(omsOrderReturnApply.getCompanyAddressId());
        //封装返回值对象
        OmsOrderReturnApplyVo vo = new OmsOrderReturnApplyVo();
        if (omsOrderReturnApply != null) {
            BeanUtils.copyProperties(omsOrderReturnApply, vo);
        }
        if (address != null) {
            BeanUtils.copyProperties(address, vo);
        }

        return vo;
    }

    /**
     * 确认退货申请,收货
     *
     * @param omsOrderReturnApply
     * @param id
     */
    public void updateStatus(OmsOrderReturnApply omsOrderReturnApply, Integer id) {
        //将id封装到OmsOrderReturnApply
        omsOrderReturnApply.setId(Long.valueOf(String.valueOf(id)));
        omsOrderReturnApply.setReceiveTime(new DateTime(System.currentTimeMillis()));
        omsOrderReturnApplyApi.updateStatus(omsOrderReturnApply);

    }
}
